package com.boby.homecamera.util;

import android.graphics.Bitmap;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class FileUtil {

    /**
     * 保存图片到指定位置上
     * @param bitmap
     * @param dirPath
     * @param fileName
     * @return
     */
    public static String saveBitmapToDir(Bitmap bitmap, String dirPath, String fileName) {
        if (bitmap != null && !"".equalsIgnoreCase(dirPath)) {

            File bitmapFile ;
            if(dirPath == null || dirPath.isEmpty()){
                bitmapFile = new File(fileName);
            }else {
                File dirFile = new File(dirPath);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                bitmapFile = new File(dirPath + "/" + fileName + ".jpeg");
            }

            try {
                BufferedOutputStream bos;
                bos = new BufferedOutputStream(new FileOutputStream(bitmapFile));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                bos.close();
                return bitmapFile.getPath();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static String saveThumbnailToDir(Bitmap bitmap, String dirPath, String fileName,int targetWidth, int targetHeight) {
        Bitmap thumbnail = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
        return saveBitmapToDir(thumbnail, dirPath, fileName);
    }

}
